//
//  BMSAuthManager.m
//  WDBMS
//
//  Created by YeJary on 2025/5/12.
//

#import "BMSAuthManager.h"
#import "BMSPrivacyPopupView.h"
#import "BMSPopupQueueManager.h"
#import "BMSRealnamePopupView.h"
#import "BMSPrivacyExitView.h"
#import "BMSAntiAddictionPopupView.h"
#import "BMSLifecycleManager.h"
#import "BMSInitializer.h"
#import "BMSOnlineSessionManager.h"
#import "BMSPrivate.h"
@interface BMSAuthManager()


@end

@implementation BMSAuthManager

/// 触发隐私弹窗，回调用户是否同意
- (void)showPrivacyPopupWithCompletion:(void (^)(BOOL agreed))completion {
    BMSPrivacyPopupView *popup = [[BMSPrivacyPopupView alloc] init];
    popup.completion = ^(BOOL agreed) {
        [[BMSPopupQueueManager sharedManager] dismissCurrentPopup];
        if (completion) {
            completion(agreed); // ✅ 结果回传
        }
    };
    [[BMSPopupQueueManager sharedManager] presentPopup:popup];
}
- (void)showRealnamePopupWithCompletion:(void(^)(BOOL success))completion {
    BMSRealnamePopupView *popup = [[BMSRealnamePopupView alloc] init];
    popup.onResult = ^(BOOL success, NSError *error) {
        // 关闭弹窗
        [[BMSPopupQueueManager sharedManager] dismissCurrentPopup];
        if(!success){
            [BMSAuthManager showForceExitPopup];
        } else {
            [[BMSOnlineSessionManager shared]stopTracking];
        }
        // 回调结果
        if (completion) {
            completion(success);
        }
    };

    [[BMSPopupQueueManager sharedManager] presentPopup:popup];
}

- (void)showExitPopupWithCompletion:(void (^)(BOOL agreed))completion {
    BMSPrivacyExitView *popup = [[BMSPrivacyExitView alloc] init];
    popup.completion = ^(BOOL agreed) {
        [[BMSPopupQueueManager sharedManager] dismissCurrentPopup];
        if (completion) {
            completion(agreed); // ✅ 结果回传
        }
    };
    [[BMSPopupQueueManager sharedManager] presentPopup:popup];
}

+ (void)showForceExitPopup {
    BMSAntiAddictionPopupView *popup = [[BMSAntiAddictionPopupView alloc] init];
    popup.onConfirm = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            exit(0);
        });
    };
    [[BMSPopupQueueManager sharedManager] presentPopup:popup];
}

@end
